﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using TRPOP.CRM.Component.Model;
using TRPOP.Common;

namespace TRPOP.CRM.Component.DALSQLServer
{
    public class SystemRoleDAL
    {

        private static TRPOP.ComSqlDAL.ICommonDAL CommonDAL = new TRPOP.ComSqlDAL.CommonDAL();
        #region 私有的方法
        ///	<summary>
        ///	从Table取数据到实体
        ///	</summary>
        ///	<param name="oDataRow">数据行</param>
        ///	<returns>返回实体</returns>
        private static SystemRoleInfo ParseRowTomodel(DataRow oDataRow)
        {
            SystemRoleInfo oInfo = new SystemRoleInfo();
            oInfo.nvcRoleName = oDataRow["nvcRoleName"] is DBNull ? "" : (string)oDataRow["nvcRoleName"];
            oInfo.nvcDiscription = oDataRow["nvcDiscription"] is DBNull ? "" : (string)oDataRow["nvcDiscription"];
            oInfo.dtCreationTime = oDataRow["dtCreationTime"] is DBNull ? (new DateTime(1900, 1, 1)) : (DateTime)oDataRow["dtCreationTime"];
            oInfo.nvcRemark = oDataRow["nvcRemark"] is DBNull ? "" : (string)oDataRow["nvcRemark"];
            oInfo.iSystemRoleID = oDataRow["iSystemRoleID"] is DBNull ? 0 : (int)oDataRow["iSystemRoleID"];
            return oInfo;
        }


        ///	<summary>
        ///	通用的添加存储过程的变量
        ///	</summary>
        ///	<param name="osdDataBase">SqlDatabase</param>
        ///	<param name="odbCommand">DbCommand</param>
        ///	<param name="oInfo">实体</param>
        private static SystemRoleInfo ParseModeltoParam(SqlDatabase odbDataBase, DbCommand odbCommand, SystemRoleInfo oInfo)
        {
            odbDataBase.AddInParameter(odbCommand, "@nvcRoleName", System.Data.DbType.String, oInfo.nvcRoleName);
            odbDataBase.AddInParameter(odbCommand, "@nvcDiscription", System.Data.DbType.String, oInfo.nvcDiscription);
            odbDataBase.AddInParameter(odbCommand, "@dtCreationTime", System.Data.DbType.DateTime, oInfo.dtCreationTime);
            odbDataBase.AddInParameter(odbCommand, "@nvcRemark", System.Data.DbType.String, oInfo.nvcRemark);
            odbDataBase.AddParameter(odbCommand, "@iSystemRoleID", System.Data.DbType.Int32, ParameterDirection.InputOutput, null, DataRowVersion.Default, oInfo.iSystemRoleID);
            return oInfo;
        }


        #endregion

        ///	<summary>
        ///	 更新或添加
        ///	</summary>
        ///	<param name="oInfo"></param>
        ///	<returns>bool</returns>
        public bool Save(SystemRoleInfo oInfo)
        {
            bool flag = false;
            try
            {
                SqlDatabase odbDataBase = new SqlDatabase(SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DbCommand odbCommand = odbDataBase.GetStoredProcCommand("SystemRole_Save");
                ParseModeltoParam(odbDataBase, odbCommand, oInfo);
                odbDataBase.ExecuteNonQuery(odbCommand);
                oInfo.iSystemRoleID = (int)odbDataBase.GetParameterValue(odbCommand, "@iSystemRoleID");
                flag = true;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return flag;
        }

        ///	<summary>
        ///	根据Primary Key获取信息实体
        ///	</summary>
        ///	<param name="intiSystemRoleID"></param>
        ///	<returns>SystemRoleInfo</returns>
        public SystemRoleInfo GetInfoById(int intiSystemRoleID)
        {
            SystemRoleInfo oInfo = null;
            string strSqlString = "SELECT * FROM SystemRole WHERE  iSystemRoleID=" + intiSystemRoleID + "    ";
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSqlString, SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                if (oDataTable.Rows.Count == 1)
                {
                    oInfo = ParseRowTomodel(oDataTable.Rows[0]);
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oInfo;
        }

        ///	<summary>
        ///	 根据Primary Key删除信息实体
        ///	</summary>
        ///	<param name="intiSystemRoleID"></param>
        ///	<returns>bool</returns>
        public bool DeleteInfoById(int intiSystemRoleID)
        {
            string strSqlString = " UPDATE SystemRole SET bIsDelete=1 WHERE   iSystemRoleID=" + intiSystemRoleID + "    ";

            try
            {
                return CommonDAL.ExeSQLQuery(strSqlString, SystemInfoHelper.getSystemInfo().DataBaseConnection) == EnumSubmitResult.Success;
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
        }

        ///	<summary>
        ///	 获取信息列表（无分页）
        ///	</summary>
        ///	<param name="intTopn">前N条，为0时表示所有符合条件的</param>
        ///	<param name="strCondition">自定义条件，要以And开头</param>
        ///	<param name="strOrderby">排序，不需要Order by </param>
        ///	<returns>List<SystemRoleInfo></returns>
        public List<SystemRoleInfo> GetListByCondition(int intTopn, string strCondition, string strOrderby)
        {
            List<SystemRoleInfo> oList = new List<SystemRoleInfo>();

            strCondition += " AND bIsDelete=0 ";

            string strSql = CommonDAL.strbuildSqlForList(intTopn, strCondition, strOrderby, "SystemRole");
            try
            {
                DataSet oDataSet = CommonDAL.GetDataSet(strSql, SystemInfoHelper.getSystemInfo().DataBaseConnection);
                DataTable oDataTable = oDataSet.Tables[0];
                foreach (DataRow otempRow in oDataTable.Rows)
                {
                    oList.Add(ParseRowTomodel(otempRow));
                }
            }
            catch (Exception oeException)
            {
                throw new Exception(oeException.Message, oeException);
            }
            return oList;
        }
    }
}
